<template>
  <el-form :model="staff" :rules="rules" ref="staff" label-width="100px" class="demo-staff">
    <el-form-item label="姓名" prop="name">
      <el-input v-model="staff.name"></el-input>
    </el-form-item>
    <el-form-item label="工号/账号" prop="account">
      <el-input v-model="staff.account"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="staff.password"></el-input>
    </el-form-item>

    <el-form-item label="收银权限" prop="Pcash">
      <el-switch v-model="staff.Pcash"></el-switch>
    </el-form-item>
    <el-form-item label="采购权限" prop="Ppurchase">
      <el-switch v-model="staff.Ppurchase"></el-switch>
    </el-form-item>
    <el-form-item label="运营权限" prop="Pplan">
      <el-switch v-model="staff.Pplan"></el-switch>
    </el-form-item>
    <el-form-item label="授权权限" prop="Pimpower">
      <el-switch v-model="staff.Pimpower"></el-switch>
    </el-form-item>
    <el-form-item label="经理权限" prop="Proot">
      <el-switch v-model="staff.Proot"></el-switch>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('staff')">立即创建</el-button>
      <el-button @click="resetForm('staff')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import {Employees,addEmployee} from "../models/employee";

export default {
  name: "NewStaff",
  props:['successCallBack'],
  data() {
    return {
      staff: Employees.createNew(),
      rules: {
        name: [
          { required: true, message: '请输入员工', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        account:[
          {required:true,message:'请输入账号',trigger:'blur'},
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' },
        ],
        password:[
          {required:true,message:'请输入账号',trigger:'blur'},
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' },
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.staff.senior=this.$store.state.staff.account;
          addEmployee(this.staff).then((success)=>{
            this.$message({
              type:"success",
              message:'员工新增完成',
            });
            this.successCallBack();
          },
          failed=>{
            this.$message({
              type:'warning',
              message:'员工账号已存在',
            })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>

</style>
